﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Collections.Specialized;

namespace LightSwitchApplication
{
    public partial class Traingroups
    {
        partial void propInterval_Changed()
        {
            if (this.propInterval != null && 
                this.propInterval.Name != null)
            {
                setIsEnabledSavely("gridIntervallValues", true);
            }
            else
            {
                setIsEnabledSavely("gridIntervallValues", false);
            }
        }

        partial void tlTraingroupSet_SelectionChanged()
        {
            if (this.tlTraingroupSet.SelectedItem != null &&
                this.tlTraingroupSet.SelectedItem.trIntervalValueTraingroupSetItem.Count() > 0)
            {
                propInterval = 
                    this.tlTraingroupSet.SelectedItem.trIntervalValueTraingroupSetItem.FirstOrDefault().tlIntervalValue.tlInterval;
                setIsEnabledSavely("cbxPropInterval", false);
            }
            else
            {
                propInterval = null;
                setIsEnabledSavely("cbxPropInterval", true);
            }
        }

        partial void propInterval_Validate(ScreenValidationResultsBuilder results)
        {
            if (this.tlTraingroupSet.SelectedItem == null)
                return;

            Microsoft.LightSwitch.Framework.EntityCollection<trIntervalValueTraingroupSetItem> IntervalValues = this.tlTraingroupSet.SelectedItem.trIntervalValueTraingroupSetItem;

            if (IntervalValues.Count() > 0)
            {
                List<tlInterval> lIntervals = new List<tlInterval>();

                foreach(trIntervalValueTraingroupSetItem item in IntervalValues)
                {
                    if (item.tlIntervalValue == null)
                        continue;
                    tlInterval Interval = item.tlIntervalValue.tlInterval;
                    if (!lIntervals.Contains(Interval))
                        lIntervals.Add(Interval);
                }
                if (lIntervals.Count > 1)
                {
                    results.AddPropertyError("Intervallwerte verschiedener Intervalle werden nicht unterstützt!");
                    this.FindControl("gridIntervallValues").IsEnabled = true;
                }
            }
                
            // results.AddPropertyError("<Fehlermeldung>");
        }

        partial void trIntervallValueTraingroupCollection_Changed(NotifyCollectionChangedEventArgs e)
        {
            if (this.tlTraingroupSet.SelectedItem == null)
                return;
            if (this.tlTraingroupSet.SelectedItem.trIntervalValueTraingroupSetItem.Count() <= 0)
            {
                setIsEnabledSavely("cbxPropInterval", true);
            }
            else
            {
                setIsEnabledSavely("cbxPropInterval", false);
            }
        }

        /// <summary>
        /// This method checks the state of IsEnabled before  changing it.
        /// This is done to prevent loops by changed properties.
        /// ToDo: Extend method to deal with any property and move it in helper class.
        /// </summary>
        /// <param name="strControl"></param>
        /// <param name="bIsVisible"></param>
        private void setIsEnabledSavely(string strControl, bool bIsVisible)
        {
            if (this.FindControl(strControl).IsVisible != bIsVisible)
                this.FindControl(strControl).IsVisible = bIsVisible;
        }
    }
}
